package com.avengers.core.bean.enums;

import java.math.BigDecimal;

/**
 * @author yu.wang
 * @since 2019/3/13 11:12 AM
 **/
public enum FileSize {
    // byte
    B(1, BaseFileSize.B),
    // kb
    K(1, BaseFileSize.K),
    K5(5, BaseFileSize.K),
    K10(10, BaseFileSize.K),
    K50(50, BaseFileSize.K),
    K128(128, BaseFileSize.K),
    K900(900, BaseFileSize.K),
    M(1, BaseFileSize.M),
    M2(2, BaseFileSize.M),
    M5(5, BaseFileSize.M),
    M10(10, BaseFileSize.M),
    M50(50, BaseFileSize.M),
    M100(100, BaseFileSize.M),
    G(1, BaseFileSize.G),
    T(1, BaseFileSize.T);

    public final BigDecimal length;

    FileSize(int multiple, BaseFileSize baseSize) {
        this.length = baseSize.length.multiply(BigDecimal.valueOf(multiple));
    }

    public boolean biggerThan(long size) {
        return biggerThan(BigDecimal.valueOf(size));
    }

    public boolean biggerThan(BigDecimal size) {
        if (null == size) {
            return false;
        }
        return length.compareTo(size) > 0;
    }

    public boolean smallerThan(long size) {
        return smallerThan(BigDecimal.valueOf(size));
    }

    public boolean smallerThan(BigDecimal size) {
        if (null == size) {
            return false;
        }
        return length.compareTo(size) < 0;
    }

    enum BaseFileSize {
        B(1),
        K(1024),
        M(1024 * 1024L),
        G(1024 * 1024 * 1024L),
        T(1024 * 1024 * 1024 * 1024L);
        final BigDecimal length;

        BaseFileSize(long length) {
            this.length = BigDecimal.valueOf(length);
        }
    }
}
